package org.example;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
 * Hello world!
 */
public class batHandle {

    public static String token = "8b2682dcf9c5443c8f7e5a751f2b6d84";

    public static void main(String[] args) throws IOException {

        long now = System.currentTimeMillis();

        while (true) {
            int page = 1;
            /*告警列表的http请求路径 一次拿100条数据 并且处理状态都是未处理的*/
            String alarmListUrl = "https://bi.baishucloud.com/datalink-api/admin-api/bi/" +
                    "task-alarm-info/page?pageNo=" + page + "&pageSize=50&alarmStatus=&dealStatus=1";

            /*创建 GET http请求 拿到返回值*/
            Map request = createGetHttpRequest(alarmListUrl, token);
            /*开始处理返回值*/
            checkCode(request);
            Map data = (Map) request.get("data");
            ArrayList<Map> list = (ArrayList<Map>) data.get("list");
            for (Map map : list) {
                {
                    /*因为程序每20分钟运行一次，所以只处理当前时间30分钟前的数据 以保证没有遗漏的数据*/
                    Long handleStartTime = now - 30L * 60 * 1000;
                    if ((Long) map.get("createTime") < now && (Long) map.get("createTime") > handleStartTime) {
                        /*符合时间段的采取处理*/
                        String findByTaskLogIdUrl =
                                "https://bi.baishucloud.com/admin-api/bi/ding-dataset-task-log/" +
                                        "page?pageNo=1&pageSize=10&" +
                                        "taskId=" + map.get("taskId") + "&taskLogId=" + map.get("taskLogId");
                        Map getHttpRequest = createGetHttpRequest(findByTaskLogIdUrl, token);
                        checkCode(getHttpRequest);
                        Map data1 = (Map) getHttpRequest.get("data");
                        ArrayList<Map> list1 = (ArrayList<Map>) data1.get("list");
                        if (list1 != null && !list1.isEmpty()) {
                            Map map1 = list1.get(0);
                            //exceptionMessage
                            String exceptionMessage = (String) map1.get("exceptionMessage");
                            if (exceptionMessage.contains("钉钉接口波动")) {
                                /*调用接口执行*/
                                String fireEventUrl =
                                        "https://bi.baishucloud.com/admin-api/bi/" +
                                                "ding-dataset-task/fireTaskLogId?taskLogId=" + map.get("taskLogId");
                                Map getHttpRequest1 = createGetHttpRequest(fireEventUrl, token);
                                checkCode(getHttpRequest1);
                            } else {
                                System.out.println("出现不也是接口波动所造成的异常");
                            }
                        }
                    } else {
                        break;
                    }
                }
            }
            page++;
        }
    }

    private static void checkCode(Map request) {
        Integer code = (Integer) request.get("code");
        if (!code.equals(0)) {
            throw new RuntimeException("请求失败");
        }
    }

    private static Map createGetHttpRequest(String alarmListUrl, String token) throws IOException {
        CloseableHttpResponse response;
        String result = "";
        CloseableHttpClient httpClient;


//        httpClient = HttpClients.createDefault();
        httpClient = HttpClients.createDefault();
        // 创建一个 HttpGet 实例
        HttpGet httpGet = new HttpGet(alarmListUrl);
        // 设置请求头信息，鉴权
        httpGet.setHeader("Authorization", "Bearer " + token);
        // 设置配置请求参数
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                .setConnectionRequestTimeout(35000)// 请求超时时间
                .setSocketTimeout(60000)// 数据读取超时时间
                .build();
        // 为httpGet实例设置配置
        httpGet.setConfig(requestConfig);
        // 执行get请求得到返回对象
        response = httpClient.execute(httpGet);
        // 通过返回对象获取返回数据
        HttpEntity entity = response.getEntity();
        // 通过EntityUtils中的toString方法将结果转换为字符串
        result = EntityUtils.toString(entity);
        /*result 转成 Map*/
        Gson gson = new Gson();
        return gson.fromJson(result, Map.class);
    }

}
